package com.dongnao.network.netty_packet;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

public class MyMessageDecoder extends ReplayingDecoder<MessageProtocol> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println("decode被调用");

        int i = byteBuf.readInt();
        byte[] content = new byte[i];
        byteBuf.readBytes(content);

        //封装成MessageProtocol对象，传给下一个处理器
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setContent(content);
        messageProtocol.setLen(i);

        list.add(messageProtocol);
    }
}
